package com.kedacom.freedomLabel.viewmodel;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.OnLifecycleEvent;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.kedacom.freedomLabel.AppExecutors;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.initdata.bean.MachineNumber;
import com.kedacom.freedomLabel.model.bean.Config;
import com.kedacom.freedomLabel.model.bean.ConfigKeys;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.DataCallback;
import com.kedacom.freedomLabel.model.db.DataLoader;
import com.kedacom.freedomLabel.model.db.dao.ConfigDao;

import java.util.List;

public class BasicInformationViewModel extends BaseViewModel {

    public MutableLiveData<Config> mLiveDeviceCode = new MutableLiveData<>();

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void prepareBasicInformationConfiguration() {
        ConfigDao dao = AppDatabase.getInstance().configDao();
        DataLoader.get(dao, ConfigKeys.DEVICE_CODE, new DataCallback<List<Config>>() {
            @Override
            public void success(@Nullable List<Config> data) {
                if (data == null || data.isEmpty()) {
                    mLiveDeviceCode.setValue(null);
                } else {
                    mLiveDeviceCode.setValue(data.get(0));
                }
            }

            @Override
            public void error(@Nullable String errorMessage) {
                mLiveDeviceCode.setValue(null);
                showToast(errorMessage, TOAST_ERROR);
            }
        });
    }

    public void saveDeviceCode(String deviceCode) {
        Config config = mLiveDeviceCode.getValue();
        ConfigDao dao = AppDatabase.getInstance().configDao();
        if (config == null) {
            config = new Config(ConfigKeys.DEVICE_CODE, deviceCode);
        } else {
            config.configValue = deviceCode;
        }
        DataLoader.save(dao, config);
    }


    public void DeleteMachine( String machineNumberName) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    AppDatabase.getInstance().getMachineNumDao()
                            .delete(machineNumberName);
                    List<MachineNumber> machineNumberList = AppDatabase.getInstance().getMachineNumDao()
                            .get();
                    sendMessage("getMachineList",machineNumberList);
                });
    }
    public void InsertMachine(MachineNumber machineNumber) {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    List<MachineNumber> machineNumberList = AppDatabase.getInstance().getMachineNumDao()
                            .get(machineNumber.getMachineName());
                    if(machineNumberList.size()==1){
                       showToast("已存在",TOAST_WARNING);
                        return;
                    }
                    AppDatabase.getInstance().getMachineNumDao()
                            .save(machineNumber);

                    List<MachineNumber> machineNumberListtwo = AppDatabase.getInstance().getMachineNumDao()
                            .get();
                    sendMessage("getMachineList",machineNumberListtwo);
                });
    }

    public void querAllMachine() {
        AppExecutors.getInstance().queryIO()
                .submit(() -> {
                    List<MachineNumber> machineNumberList = AppDatabase.getInstance().getMachineNumDao()
                            .get();
                    sendMessage("getMachineList",machineNumberList);
                });
    }

}